<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//类型属性控制器
class FattrController extends CommonController{
	private $fattr_model;
	private $form_model;
	public function __init()
	{
		//调用CommonController里的构造函数
		parent::__init();
		//实例化属性fattr模型
		$this->fattr_model = new \Common\Model\Fattr;
		//实例化类型form模型
		$this->form_model = new \Common\Model\Form;
	}
	
	public function index(){
		//获取类型ID
		$fid = Q('get.fid',0,'intval');
		
		//分页
		$total = $this->fattr_model->join('form','fid','=','form_fid')->where("form_fid={$fid}")->groupBy('fname')->count();
		$page = Page::row(12)->make($total);
		View::with('page',$page);
		
		$formData = $this->form_model->where("fid={$fid}")->find();
		View::with('formData',$formData);
	
		//获取数据
        $data = $this->fattr_model->join('form','fid','=','form_fid')->where("form_fid={$fid}")->limit(Page::limit())->get();
        View::with('data',$data);
		
		View::with('fid',$fid);
		View::make();
	}
	//添加属性
	public function add(){
		//获取类型ID
		$fid = Q('get.fid',0,'intval');

		if(IS_POST){
			if(!$this->fattr_model->store()) View::error($this->fattr_model->getError());
			View::success('属性添加成功',U('index',array('fid'=>$fid)));
		}

		//用于页面导航
		$formData = $this->form_model->where("fid={$fid}")->find();
		View::with('formData',$formData);
		View::with('fid',$fid);
		View::make();
	}
	//编辑
	public function edit(){
		//2.修改
		if(IS_POST){
			if(!$this->fattr_model->edit())	View::error($this->fattr_model->getError());
			View::success('修改成功',U('index',array('fid'=>Q('post.fid',0,'intval'))));	 
		}

		//1.获得旧数据
		$oldData = $this->fattr_model->where("fa_id=" . Q('get.fa_id',0,'intval'))->find();
		View::with('oldData',$oldData);
		
		//用于页面导航
		$fid = $oldData['form_fid'];
		View::with('fid',$fid);
		$formData = $this->form_model->where("fid={$fid}")->find();
		View::with('formData',$formData);
		
		View::make();
	}
	//删除
	public function del(){
		$this->fattr_model->where("fa_id=" . Q('get.fa_id',0,'intval'))->delete();
		View::success('删除成功');
	}
}

 ?>